<?php 
//I.F. Nuevo: 2010-03-28; ramarin
require_once('../../../config.php');
require_once($CFG->libdir.'/gdlib.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->dirroot.'/user/editlib.php');
require_once($CFG->dirroot.'/user/profile/lib.php');

function categories_moosisca_remove() {
  if (!delete_records('moosisca_categories')) {
    return false;
  }

  //trigger category events
  $eventdata = new object();
  events_trigger('moosisca_categories_removed', $eventdata);

  return true;
}

function category_moosisca_add($category_id, $cod1, $cod2) {
  $category = new object();
  $category->category_id  = $category_id;
  $category->cod1         = $cod1;
	$category->cod2         = $cod2;

  if (!insert_record('moosisca_categories', $category, false)) {
    return false;
  }
  //trigger category events
  events_trigger('category_added', $category);

  return true;
}

/**
 * Returns a sorted list of categories. Each category object has a context
 * property that is a context object.
 *
 * When asking for $parent='none' it will return all the categories, regardless
 * of depth. Wheen asking for a specific parent, the default is to return
 * a "shallow" resultset. Pass false to $shallow and it will return all
 * the child categories as well.
 *
 *
 * @param string $parent The parent category if any
 * @param string $sort the sortorder
 * @param bool   $shallow - set to false to get the children too
 * @return array of categories
 */
function get_categories_moosisca($parent='none', $sort=NULL, $shallow=true) {
    global $CFG;

    if ($sort === NULL) {
        $sort = 'ORDER BY cc.sortorder ASC';
    } elseif ($sort ==='') {
        // leave it as empty
    } else {
        $sort = "ORDER BY $sort";
    }
    
    if ($parent === 'none') {
        $sql = "SELECT cc.*,
                      ctx.id AS ctxid, ctx.path AS ctxpath,
                      ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
                FROM {$CFG->prefix}course_categories cc
                JOIN {$CFG->prefix}context ctx
                  ON cc.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSECAT."
                $sort";
    } elseif ($shallow) {
        $parent = (int)$parent;
        $sql = "SELECT cc.*,
                       ctx.id AS ctxid, ctx.path AS ctxpath,
                       ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel,
                       mcc.category_id, mcc.cod1, mcc.cod2
                FROM {$CFG->prefix}course_categories cc
                JOIN {$CFG->prefix}context ctx
                  ON cc.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSECAT."
                LEFT JOIN {$CFG->prefix}moosisca_categories mcc
                  ON mcc.category_id = cc.id
                WHERE cc.parent=$parent
                $sort";
    } else {
        $parent = (int)$parent;
        $sql = "SELECT cc.*,
                       ctx.id AS ctxid, ctx.path AS ctxpath,
                       ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
                FROM {$CFG->prefix}course_categories cc
                JOIN {$CFG->prefix}context ctx
                  ON cc.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSECAT."
                JOIN {$CFG->prefix}course_categories ccp
                     ON (cc.path LIKE ".sql_concat('ccp.path',"'%'").")
                WHERE ccp.id=$parent
                $sort";
    }
    $categories = array();

    if( $rs = get_recordset_sql($sql) ){
        while ($cat = rs_fetch_next_record($rs)) {
            $cat = make_context_subobj($cat);
            if ($cat->visible || has_capability('moodle/category:viewhiddencategories',$cat->context)) {
                $categories[$cat->id] = $cat;
            }
        }
    }
    return $categories;
} 

function get_categories_tree(){
  global $CFG;
  
  $sql = "SELECT path
    FROM {$CFG->prefix}course_categories cc
    INNER JOIN {$CFG->prefix}moosisca_categories mc ON cc.id = mc.category_id";

  $tree = array();
    
  if( $rs = get_recordset_sql($sql) ){
    while ($cat = rs_fetch_next_record($rs)) {
      $tree[] = $cat->path;
    }
  }
  
  return $tree;
}